/*
 * TitleFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;

import javax.swing.*;

import com.google.code.timetrail.backend.*;
import com.google.code.timetrail.presenter.TitleFrameBackend;

import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class TitleFrame extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Control myGameControl;
	private JPanel myCD;
	private TitleFrameBackend titleFrameBackend;
	private ImageIcon title;
	private ImageIcon background;
	private ImageIcon star;
	private JButton newGameButton;
	private JButton loadGameButton;

	public TitleFrame(Control gameControl, JPanel cd) {
		myGameControl = gameControl;
		myCD = cd;
		titleFrameBackend = new TitleFrameBackend(myGameControl);

		title = titleFrameBackend.getTitle();
		background = titleFrameBackend.getBackground();
		star = titleFrameBackend.getStar();

		newGameButton = new JButton(titleFrameBackend.getNewGameButtonText());
		loadGameButton = new JButton(titleFrameBackend.getLoadGameButtonText());

		newGameButton.setOpaque(false);
		newGameButton.setContentAreaFilled(false);
		newGameButton.setBorderPainted(false);

		newGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				newGameButton.setIcon(titleFrameBackend.getNewGameButtonTextHover());
			}

			public void mouseExited(java.awt.event.MouseEvent evt) {
				newGameButton.setIcon(titleFrameBackend.getNewGameButtonText());
			}
		});
		newGameButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				myCD.add(new StartupFrame(myGameControl, myCD),
						"name_131784086749183704");
				CardLayout cd = (CardLayout) myCD.getLayout();
				cd.last(myCD);
			}
		});

		loadGameButton.setOpaque(false);
		loadGameButton.setContentAreaFilled(false);
		loadGameButton.setBorderPainted(false);

		loadGameButton.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseEntered(java.awt.event.MouseEvent evt) {
				loadGameButton.setIcon(titleFrameBackend.getLoadGameButtonTextHover());
			}

			public void mouseExited(java.awt.event.MouseEvent evt) {
				loadGameButton.setIcon(titleFrameBackend.getLoadGameButtonText());
			}
		});
		loadGameButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			    try {
                    myCD.add(new LoadGameFrame(myGameControl, myCD), "TakeMove");
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
			}
		});
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				Alignment.TRAILING,
				groupLayout.createSequentialGroup()
						.addContainerGap(61, Short.MAX_VALUE)
						.addComponent(newGameButton).addGap(50)
						.addComponent(loadGameButton).addGap(61)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addContainerGap(340, Short.MAX_VALUE)
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				loadGameButton)
																		.addContainerGap())
														.addGroup(
																Alignment.TRAILING,
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				newGameButton)
																		.addGap(32)))));
		setLayout(groupLayout);
	}

	public void paintComponent(Graphics g) {
		int height = this.getSize().height;
		int width = this.getSize().width;

		g.drawImage(background.getImage(), 0, 0, width, height, this);
		g.drawImage(title.getImage(), width / 9, height / 6, 3 * width / 4,
				height / 2, this);

		// I picked random locations, we can code this with an algorithm if you
		// really want
		g.drawImage(star.getImage(), width / 12, height / 5, this);
		g.drawImage(star.getImage(), width / 6, 7 * height / 10, this);
		g.drawImage(star.getImage(), 5 * width / 6, height / 8, this);
		g.drawImage(star.getImage(), 53 * width / 60, height / 2, this);
	}
}
